import {
  fetchOrders,
  fetchOrdersCount,
} from '../../../services/order/orderList';
import { cosThumb } from '../../../utils/util';
import { fetchCommodityCollectionList, fetchEssayCollectionList, fetchEssayLikeList } from '../../../utils/request';

Page({
  page: {
    size: 5,
    num: 1,
  },

  data: {
    tabs: [
      { key: 1, text: '商品收藏', info: '' },
      { key: 2, text: '帖子收藏', info: '' },
      { key: 3, text: '帖子点赞', info: '' }
    ],
    curTab: 1,
    orderList: [],
    listLoading: 0,
    pullDownRefreshing: false,
    emptyImg:
      'https://cdn-we-retail.ym.tencent.com/miniapp/order/empty-order-list.png',
    backRefresh: false,
    status: 1,
  },

  onLoad() {
    this.init(1);
    this.pullDownRefresh = this.selectComponent('#wr-pull-down-refresh');
  },

  onShow() {
    if (!this.data.backRefresh) return;
    this.onRefresh();
    this.setData({ backRefresh: false });
  },

  onReachBottom() {
    if (this.data.listLoading === 0) {
      this.getData(this.data.curTab);
    }
  },

  onPageScroll(e) {
    this.pullDownRefresh && this.pullDownRefresh.onPageScroll(e);
  },

  onPullDownRefresh_(e) {
    const { callback } = e.detail;
    this.setData({ pullDownRefreshing: true });
    this.refreshList(this.data.curTab)
      .then(() => {
        this.setData({ pullDownRefreshing: false });
        callback && callback();
      })
      .catch((err) => {
        this.setData({ pullDownRefreshing: false });
        Promise.reject(err);
      });
  },

  init(status) {
    status = status !== undefined ? status : this.data.curTab;
    this.setData({
      status,
    });
    this.refreshList(status);
  },

  getData(statusCode = 1, reset = false) {
    const params = {
      parameter: {
        pageSize: this.page.size,
        pageNum: this.page.num,
      },
    };
    this.setData({ listLoading: 1 });
    if (statusCode == 2) {
      return fetchEssayCollectionList(params.parameter)
      .then((res) => {
        this.page.num++;
        let orderList = [];
        if (res && res.data && res.data.content) {
          orderList = res.data.content;
        }
        return new Promise((resolve) => {
          if (reset) {
            this.setData({ orderList: [] }, () => resolve());
          } else resolve();
        }).then(() => {
          this.setData({
            orderList: this.data.orderList.concat(orderList),
            listLoading: orderList.length > 0 ? 0 : 2,
          });
        });
      })
      .catch((err) => {
        this.setData({ listLoading: 3 });
        return Promise.reject(err);
      });
    } 
    if (statusCode == 3) {
      return fetchEssayLikeList(params.parameter)
      .then((res) => {
        this.page.num++;
        let orderList = [];
        if (res && res.data && res.data.content) {
          orderList = res.data.content;
        }
        return new Promise((resolve) => {
          if (reset) {
            this.setData({ orderList: [] }, () => resolve());
          } else resolve();
        }).then(() => {
          this.setData({
            orderList: this.data.orderList.concat(orderList),
            listLoading: orderList.length > 0 ? 0 : 2,
          });
        });
      })
      .catch((err) => {
        this.setData({ listLoading: 3 });
        return Promise.reject(err);
      });
    } 
    return fetchCommodityCollectionList(params.parameter)
      .then((res) => {
        this.page.num++;
        let orderList = [];
        if (res && res.data && res.data.content) {
          orderList = res.data.content;
        }
        return new Promise((resolve) => {
          if (reset) {
            this.setData({ orderList: [] }, () => resolve());
          } else resolve();
        }).then(() => {
          this.setData({
            orderList: this.data.orderList.concat(orderList),
            listLoading: orderList.length > 0 ? 0 : 2,
          });
        });
      })
      .catch((err) => {
        this.setData({ listLoading: 3 });
        return Promise.reject(err);
      });
  },

  onReTryLoad() {
    this.getData(this.data.curTab);
  },

  onTabChange(e) {
    const { value } = e.detail;
    this.setData({
      status: value,
    });
    this.refreshList(value);
  },

  refreshList(status = 1) {
    this.page = {
      size: this.page.size,
      num: 1,
    };
    this.setData({ curTab: status, orderList: [] });

    return Promise.all([
      this.getData(status, true),
    ]);
  },

  onRefresh() {
    this.refreshList(this.data.curTab);
  },

  onOrderCardTap(e) {
    console.log("onOrderCardTap", e);
    const { order } = e.currentTarget.dataset;
    const id = order.id;
    const curTab = this.data.curTab;
    if (curTab == 1) {
      wx.navigateTo({
        url: `/pages/goods/details/index?spuId=${id}`,
      });
    } else {
      wx.navigateTo({
        url: `/pages/essay-detail/essay-detail?essayId=${id}`,
      });
    }
  },
});
